const app = getApp();
const http = require('../../../utils/util.js');
const mta = require('../../../mta_sdk/mta_analysis.js');
Page({

  data: {
    baseImg: '',
    bgpic: '',
    stuSchool: '',
    stuClass: '',
    stuName: '',
    stuPic: '',
    lastImg:'',
    avaWidth:335,
    avaHeight:430,
    pid:''
  },
  onShow(){
    var cover = wx.getStorageSync('coverpic')
    if (cover || cover!='') {
      this.setData({
        lastImg: cover || '',
        stuPic: cover || ''
      })
    }
  },
  onLoad: function (opt) {
    mta.Page.init();
    console.log(opt);
    if (opt.pid) {
      this.setData({
        baseImg: `/images/${opt.pid}_list.jpg`,
        bgpic: `/images/${opt.pid}_canvas.jpg`,
        pid:opt.pid
      })
    }
    
  },
  formSubmit: function (e) {
    app.sendForm(e);
  },
  getStuSchool: function (e) {
    this.setData({
      stuSchool: e.detail.value
    })
  },
  getStuClass: function (e) {
    this.setData({
      stuClass: e.detail.value
    })
  },
  getStuName: function (e) {
    this.setData({
      stuName: e.detail.value
    })
  },
  getStuPic: function (e) {
    var me=this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        wx.navigateTo({
          url: `/pages/cropperview/cropperview?pic=${tempFilePaths}&w=${me.data.avaWidth}&h=${me.data.avaHeight}`
        })
      }
    })
  },
  toShowImg() {
    var md=this.data;
    if (md.stuSchool == '') {
      this.tip("请输入学校")
    }
    else if (md.stuClass == ''){
      this.tip("请输入班级")
    }
    else if (md.stuName == '') {
      this.tip("请输入名字")
    }
    else if (md.stuPic == '') {
      this.tip("请上传图片")
    }
    else{
      wx.navigateTo({
        url: `/pages/${this.data.pid}/${this.data.pid}_detail/${this.data.pid}_detail?school=${md.stuSchool}&class=${md.stuClass}&name=${md.stuName}&pic=${md.stuPic}&bgpic=${md.bgpic}&pid=${this.data.pid}`,
      })
    }
    
  },
  tip(txt)
  {
    wx.showToast({
      title: txt,
      icon: 'none'
    });
  },
  onUnload() {
    wx.removeStorageSync("coverpic")
  }, 
 onShareAppMessage: function () {
   return {
     title: '学生证制作',
     desc: '学生证制作',
     path: `/pages/index/index`,
    
   }
  }
})